package com.itheima.travel.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.travel.constant.RedisConstant;
import com.itheima.travel.mapper.CategoryMapper;
import com.itheima.travel.mapper.RouteMapper;
import com.itheima.travel.pojo.Route;
import com.itheima.travel.pojo.RouteExample;
import com.itheima.travel.req.AffixVo;
import com.itheima.travel.req.RouteVo;
import com.itheima.travel.service.AffixService;
import com.itheima.travel.service.RedisCacheService;
import com.itheima.travel.service.RouteService;
import com.itheima.travel.utils.BeanConv;
import com.itheima.travel.utils.EmptyUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class RouteServiceImpl implements RouteService {
    @Autowired
    private RouteMapper routeMapper;

    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    RedisCacheService redisCacheService;

    @Autowired
    private AffixService affixService;

    @Override
    public Integer addRoute(RouteVo routeVo) {
        int i = routeMapper.insert(BeanConv.toBean(routeVo, Route.class));
        return i;
    }

    @Override
    public Integer updateRoute(RouteVo routeVo) {
//        if(EmptyUtil.isNullOrEmpty(routeVo.getId())){
//            return 0;
//        }
//        int i = routeMapper.updateByPrimaryKey(BeanConv.toBean(routeVo, Route.class));
//        return i;
        if (EmptyUtil.isNullOrEmpty(routeVo.getId())){
            return 0;
        }
        int flag = routeMapper.updateByPrimaryKey(BeanConv.toBean(routeVo, Route.class));
        //修改完成后，删除之前缓存对象,让第一次查询的人放入缓存
        if (flag==1){
            redisCacheService.deleSingleCache(RedisConstant.ROUTESERVICE_FINDROUTEBYID+routeVo.getId());
            //删除图片
            redisCacheService.deleListCache(RedisConstant.AFFIXSERVICE_FINDAFFIXBYBUSINESSID +routeVo.getId());
        }
        return flag;
    }

    @Override
    public PageInfo<RouteVo> findRouteByPage(RouteVo routeVo,int pageNum, int pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        RouteExample routeExample = new RouteExample();
        RouteExample.Criteria criteria = routeExample.createCriteria();
        if(!EmptyUtil.isNullOrEmpty(routeVo.getCategoryId())){
            criteria.andCategoryIdEqualTo(routeVo.getCategoryId());
        }
        if(!EmptyUtil.isNullOrEmpty(routeVo.getRouteName())){
            criteria.andRouteNameEqualTo("%"+routeVo.getRouteName());
        }
        if(!EmptyUtil.isNullOrEmpty(routeVo.getMinPrice())){
            criteria.andPriceGreaterThan(routeVo.getMinPrice());
        }
        if(!EmptyUtil.isNullOrEmpty(routeVo.getMaxPrice())){
            criteria.andPriceLessThan(routeVo.getMaxPrice());
        }
        List<Route> routeList = routeMapper.selectByExample(routeExample);
        PageInfo<Route> pageInfo = new PageInfo<>(routeList);
        PageInfo<RouteVo> pageInfoVo = new PageInfo<>();
        BeanConv.toBean(pageInfo, pageInfoVo);
        List<RouteVo> routeVos = BeanConv.toBeanList(pageInfo.getList(), RouteVo.class);
        pageInfoVo.setList(routeVos);
        //处理图片
        for (RouteVo routeVoHandler : routeVos) {
            AffixVo affixVo = AffixVo.builder().businessId(routeVoHandler.getId()).build();
            routeVoHandler.setAffixVoList(affixService.findAffixByBusinessId(affixVo));
        }
        return pageInfoVo;
    }

    @Override
    public RouteVo findRouteById(RouteVo routeVo) {
//        Route route = routeMapper.selectByPrimaryKey(routeVo.getId());
//        return BeanConv.toBean(route, RouteVo.class);
        return redisCacheService.singleCache(()->{
            RouteVo routeVoResult = BeanConv.toBean(routeMapper.selectByPrimaryKey(routeVo.getId()), RouteVo.class);
            //走缓存那图片
            AffixVo affixVo = AffixVo.builder().businessId(routeVoResult.getId()).build();
            routeVoResult.setAffixVoList(affixService.findAffixByBusinessId(affixVo));
            return routeVoResult;
        }, RedisConstant.ROUTESERVICE_FINDROUTEBYID+routeVo.getId());
    }

    @Override
    public PageInfo<RouteVo> findSellerById(RouteVo routeVo,int pageNum, int pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        RouteExample routeExample = new RouteExample();
        routeExample.createCriteria().andSellerIdEqualTo(routeVo.getSellerId());
        List<Route> routeList = routeMapper.selectByExample(routeExample);
        PageInfo<Route> pageInfo = new PageInfo<>(routeList);
        PageInfo<RouteVo> pageInfoVo = new PageInfo<>();
        BeanConv.toBean(pageInfo, pageInfoVo);
        List<RouteVo> routeVos = BeanConv.toBeanList(pageInfo.getList(), RouteVo.class);
        pageInfoVo.setList(routeVos);
        return pageInfoVo;
    }
}
